﻿using System;
using System.Collections.Generic;
using System.Net;

namespace Toybox.NicoSharp.Seiga
{
	using Primitives;
	using Utilities;

	public partial class Theme : AbstractResource
	{

		#region Constructor

		public Theme(ContentId contentId, CookieCollection cookies = null)
			: base(contentId, cookies)
		{
		}

		public Theme(string id, CookieCollection cookies = null)
			: base(id, cookies)
		{
		}

		#endregion Constructor


		#region Protected Members

		protected virtual string ThemeUrl
		{
			get { return NicoUrls.SeigaApi + "theme/"; }
		}


		protected virtual string InfoUrl
		{
			get { return this.ThemeUrl + "info?id="; }
		}

		protected virtual string DataUrl
		{
			get { return this.ThemeUrl + "data?theme_id="; }
		}

		protected virtual string RecommendUrl
		{
			get { return this.ThemeUrl + "recommend?id="; }
		}


		protected virtual string CounterIncrementUrl
		{
			get { return this.ThemeUrl + "counter/increment"; }
		}

		protected virtual string CommentAddUrl
		{
			get { return this.ThemeUrl + "comment/add"; }
		}

		#endregion Protected Members


		#region Public Methods

		public Info GetInfo()
		{
			return this.Get<Info>(this.InfoUrl + this.ContentId.Digits);
		}

		public Data GetData()
		{
			return this.Get<Data>(this.DataUrl + this.ContentId.Digits);
		}

		public Recommend GetRecommend()
		{
			return this.Get<Recommend>(this.RecommendUrl + this.ContentId.Digits);
		}


		public IncrementResult CounterIncrement()
		{
			PostKey key = PostKey.GetKey(this.ContentId.Digits, ActionType.count, this.Cookies);

			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict.Add("key_id", this.ContentId.Digits);
			dict.Add("id", this.ContentId.Digits);
			dict.Add("h", key.hash);
			dict.Add("a", key.action);
			dict.Add("t", key.time.ToString());

			string data = Serializer.AndEqualPairSerializer(dict).Replace("_", "%5F");
			return this.Post<IncrementResult>(this.CounterIncrementUrl, data);
		}

		public CommentAddResult CommentAdd(string res_id, int frame, string command, string content)
		{
			if (string.IsNullOrEmpty(content))
				throw new ArgumentNullException("content");

			PostKey key = PostKey.GetKey(this.ContentId.Digits, ActionType.default_flash_request, this.Cookies);

			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict.Add("key_id", this.ContentId.Digits);
			dict.Add("theme_id", this.ContentId.Digits);
			dict.Add("res_id", res_id);
			dict.Add("h", key.hash);
			dict.Add("a", key.action);
			dict.Add("t", key.time.ToString());
			dict.Add("frame", frame.ToString());
			dict.Add("command", string.IsNullOrEmpty(command) ? string.Empty : Uri.EscapeDataString(command));
			dict.Add("content", Uri.EscapeDataString(content));

			string data = Serializer.AndEqualPairSerializer(dict).Replace("_", "%5F");
			return this.Post<CommentAddResult>(this.CommentAddUrl, data);
		}


		public string GetDefaultBGMPath(bool isPremium = true)
		{
			return isPremium ? base.GetDefaultBGMPath() : NicoUrls.LohasTop + "mp3/default/bgm_low.mp3";
		}

		#endregion Public Methods

	}
}
